$(document).ready(function(){
	var type=getvl("goodsType")==""?1:getvl("goodsType");
	var select=document.getElementById("goods_type");
	for(var i=0;i<select.length;i++){
		if(select[i].value==type){
			select[i].selected=true;
			break;
		}
	}
	init(type,callback,1);
});



function init(type,callback,pageNo){
	$("#thislist").html("");
	if(type==null || type==undefined){
		type=$('#goods_type option:selected') .val();
	}
	   $.ajax({  
        type : "POST",  
        url : urlcore + "/api/goodsUnit/getGoodsList", 
        dataType : "json",
        headers:{"Content-Type":"application/json;charset=UTF-8"},
        data :  JSON.stringify({
           type:type,
           pageNum:pageNo,
           pageSize:pageSize
        	}),
        success: function(data) {//data是返回的hash,key之类的值，key是定义的文件名 
        	if(data.success==true){
        		callback(data.data.list);
        		$('#thiscount').text(data.data.total);
        		$("#pager").pager({
					pagenumber: pageNo, 
					pagecount:data.data.pages,
					totalcount:data.data.total,
					buttonClickCallback: PageClick
				}); 
        	}else if (data.code == 'OVERTIME') {
                var thisUrl = window.location.href;
                if (thisUrl.indexOf('login.html') <= -1) {
                    top.window.location.href = "login.html";
                }
            } else {
                alert(data.msg);
            }
        },
        error:function(){  
            alert("上传失败");  
        }  
    }); 
	
}


//回调函数  
PageClick = function(pageclickednumber) {  
	var type=getvl("goodsType")==""?2:getvl("goodsType");
    init(type,callback,pageclickednumber);
}

var callback=function(data){
	var html="" , title="",goodsType="",goodsUrl="",markerPrice="",unitNum="",index="",gmtDatetime="",id="";
	for(var i=0;i<data.length;i++){
		id=data[i].id;
		marketPrice=data[i].marketPrice;
		goodsUrl=data[i].goodsUrl;
		title=data[i].title;
		var realgoodsUrl = goodsUrl.split("***");
		unitNum=data[i].unitNum;
		goodsType=data[i].goodType==1?"骑手商城":"商家商城";
		gmtDatetime=new Date(data[i].gmtDatetime).pattern("yyyy-MM-dd hh:mm:ss");
		++index;
	html+=  "  <tr id=\""+id+"\">\n" +
			"  \t\t<td>"+index+"</td>\n" + 
            "\t\t <td class=\"footable-visible\"><img width=\"40px\" height=\"40px\" src=\""+realgoodsUrl[0]+"\"></td>\n"+
			"  \t\t<td>"+title+"</td>\n" + 			
			"  \t\t<th>"+gmtDatetime+"</th>\n" + 
			"  \t\t<td>\n" + 
			"  \t\t\t<a class=\"btn btn-primary btn-xs\" href=\"goods_add.html?id="+id+"\"> 修改</a>\n" + 
			"   \t\t\t<a class=\"btn btn-primary btn-xs\" href=\"javascript:;\" onclick=\"doDelete("+id+")\"> 删除</a>\n" + 
			"   \t\t\t<a class=\"btn btn-primary btn-xs\" href=\"goods_details.html?id="+id+"\"> 详细</a>\n" +
			"  \t\t</td>\n" + 
			"  \t</tr>";
	}
	$("#thislist").append(html);
}


//刪除
function doDelete(id){
		   $.ajax({  
	        type : "POST",  
	        url : urlcore + "/api/goodsUnit/deleteGoodsOne?id="+id, 
	        dataType : "json",
	        headers:{"Content-Type":"application/json;charset=UTF-8"},
	        data : {},
	        success: function(data) {//data是返回的hash,key之类的值，key是定义的文件名 
	        	if(data.success==true){
	        		location.reload();
	        	}else {
	                alert(data.msg);
	            }
	        },
	        error:function(){  
	            alert("上传失败");  
	        }  
    }); 
	
}



//搜索
function searchuser() {
		var type =$('#goods_type option:selected') .val();//选中的值
	      init(type,callback,1);
}


